package zy;
/*
 *@Student 张策
 */
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Warehouse {
    public List<Dish> dishes = new ArrayList<>();
    Scanner scanner=new Scanner(System.in);
    public Warehouse(){
        this.dishes.add(new Dish(this.dishes.size()+1,"宫保鸡丁",9.0,"不辣还好吃",0));
        this.dishes.add(new Dish(this.dishes.size()+1,"彪哥小面",8.0,"加豆干好吃",0));
        this.dishes.add(new Dish(this.dishes.size()+1,"杂粮煎饼",7.0,"加肉松好吃",0));
    }
    //展示菜单
    public Dish getDish(int pos){
        return this.dishes.get(pos);
    }
    public void show(){
        System.out.println("----------食品列表---------");
        for (int i = 0; i < dishes.size(); i++) {
            System.out.println(getDish(i));
        }
        System.out.println("----------食品列表---------");
        System.out.println("   共计"+dishes.size()+"条数据");
    }
    //增加库存
    public void addDish(Dish dish) {
        dishes.add(dish);
    }
    public void cookDish() {
        show();
        System.out.println("----------新增库存----------");
        System.out.println("你想炒的菜名是:");
        String name=scanner.nextLine();
        String num;
        int nums=0;
        if (nameBool(name)){
            System.out.println("请输入要炒的数量");
             num=scanner.nextLine();
             isNum(num);
             nums=Integer.parseInt(num);
        }
        for (Dish dish : dishes) {
            if (dish.name.equals(name)) {
                dish.stock=dish.stock+nums;
                System.out.println("库存增加成功！");
                return;
            }
        }
        System.out.println("菜品不存在！");
    }
public void throwDish() {
        boolean pos=true;
        while (pos){
    System.out.println("请输入要丢弃的菜名");
    String name=scanner.nextLine();
    int nums=0;
    for (Dish dish : dishes) {
        if (dish.name.equals(name)) {
        System.out.println("请输入要丢弃的数量");
        String num=scanner.nextLine();
        isNum(num);
        nums=Integer.parseInt(num);
            if (dish.stock > nums) {
                dish.stock=dish.stock-nums;
                System.out.println("库存减少成功！");
            } else {
                System.out.println("库存不足！");
            }
            return;
        }
    }
    System.out.println("菜品不存在！请再次输入");
}
    }
    //修改菜单
    public void setDishes(){
        show();
        System.out.println("请输入要修改的食品id");
        int num;
        while (true){
            String nums=scanner.nextLine();
            isNum(nums);
            num=Integer.parseInt(nums);
            if(num<=0||num>dishes.size()){
                System.out.println("输入的id不存在请再次输入");
            }else {
                break;
            }
        }
        Dish dish = dishes.get(num - 1);
        System.out.println("------修改视频-------");
        System.out.println("请输入菜品名称:");
        String name = scanner.nextLine();
        dish.setName(name);
        System.out.println("请输入食品价格:");
        String pr=scanner.nextLine();
        isDouble(pr);
        Double pre=Double.parseDouble(pr);
        dish.setPrice(pre);
        System.out.println("请输入描述:");
        dish.setDesc(scanner.nextLine());
        System.out.println(name+"修改成功");
        dish.setStock(0);
    }
    //根据菜名判断是否存在菜
    private boolean nameBool(String name){
        for (Dish dish : dishes) {
            if (dish.name.equals(name)) {
                return true;
            }
        }
        System.out.println("该菜品不存在");
        return false;
    }
    private boolean isDouble(String input){
        while (true) {
            int choice;
            try {
                choice = Integer.parseInt(input);
                if (choice > 0 && choice < 2000000) {
                    return true;
                } else {
                    System.out.println("输入的数字不合理");
                }
            } catch (NumberFormatException e) {
                System.out.println("输入的不是数字，请重新输入！");
            }
        }
    }
    private boolean isNum(String input){
        while (true) {
            int choice;
            try {
                choice = Integer.parseInt(input);
                if (choice > 0 && choice < 2000000) {
                    return true;
                } else {
                    System.out.println("输入的数字不合理");
                }
            } catch (NumberFormatException e) {
                System.out.println("输入的不是数字，请重新输入！");
            }
        }
    }
}

